package com.example.p2pinvest.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.example.p2pinvest.R;
import com.example.p2pinvest.activity.BarChartActivity;
import com.example.p2pinvest.activity.ChongZhiActivity;
import com.example.p2pinvest.activity.GestureVerifyActivity;
import com.example.p2pinvest.activity.LineChartActivity;
import com.example.p2pinvest.activity.LoginActivity;
import com.example.p2pinvest.activity.PieChartActivity;
import com.example.p2pinvest.activity.TiXianActivity;
import com.example.p2pinvest.activity.UserInfoActivity;
import com.example.p2pinvest.bean.User;
import com.example.p2pinvest.common.BaseActivity;
import com.example.p2pinvest.common.BaseFragment;
import com.example.p2pinvest.util.BitmapUtils;
import com.example.p2pinvest.util.UIUtils;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.io.File;
import java.io.FileOutputStream;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/2
 */
public class MeFragment extends BaseFragment {

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_setting)
    ImageView ivTitleSetting;
    @BindView(R.id.iv_me_icon)
    ImageView ivMeIcon;
    @BindView(R.id.rl_me_icon)
    RelativeLayout rlMeIcon;
    @BindView(R.id.tv_me_name)
    TextView tvMeName;
    @BindView(R.id.rl_me)
    RelativeLayout rlMe;
    @BindView(R.id.recharge)
    ImageView recharge;
    @BindView(R.id.withdraw)
    ImageView withdraw;
    @BindView(R.id.ll_touzi)
    TextView llTouzi;
    @BindView(R.id.ll_touzi_zhiguan)
    TextView llTouziZhiguan;
    @BindView(R.id.ll_zichan)
    TextView llZichan;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    public void initData(String content) {
        //判断用户是否以及登录,查看本地是否有用户的登录信息
        if (isLogin()) {

        }
    }

    /**
     * 判断用户是否登录
     */
    private boolean isLogin() {
        SharedPreferences user_info = this.getActivity().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        String name = user_info.getString("name", "");
        if (TextUtils.isEmpty(name)) {
            //本地没有用户信息,登录
            doLogin();

        } else {
            //登录过了,直接加载用户信息并显示
            doUser();
        }
        return false;
    }

    /**
     * 加载用户信息并显示
     */
    private void doUser() {

        //判断是否开启了手势密码
        SharedPreferences sp = this.getActivity().getSharedPreferences("secret_protect", Context.MODE_PRIVATE);
        boolean isOpen = sp.getBoolean("isOpen", false);
        if (isOpen) {
            //如果开启需要先输入手势密码
            ((BaseActivity) this.getActivity()).gotoActivity(GestureVerifyActivity.class, null);
        }

        FragmentActivity activity = this.getActivity();
        User user = ((BaseActivity) activity).readUser();
        tvMeName.setText(user.getName());

        //判断本地是否已经保存头像的图片,如果有则不执行联网操作获取头像
        boolean isExist = readImage();
        if (isExist) {
            return;
        }

        Picasso.get()
                .load(user.getImageurl() + "?" + System.currentTimeMillis())
                .transform(new Transformation() {

                    @Override
                    public Bitmap transform(Bitmap source) {
                        //压缩处理
                        Bitmap bitmap = BitmapUtils.zoom(source, UIUtils.dp2px(62), UIUtils.dp2px(62));
                        //圆形处理
                        bitmap = BitmapUtils.circleBitmap(bitmap);
                        //回收bitmap资源
                        source.recycle();
                        return bitmap;
                    }

                    @Override
                    public String key() {
                        return ""; //需要保证返回值不能为null
                    }
                })
                .into(ivMeIcon);
    }

    /**
     * 提示用户登录
     */
    private void doLogin() {
        new AlertDialog.Builder(this.getActivity())
                .setTitle("提示")
                .setMessage("您还没用登录哦")
                .setPositiveButton("确定", (dialog, which) -> {
                    //UIUtils.toast("进入登录界面", false);
                    BaseActivity activity = (BaseActivity) MeFragment.this.getActivity();
                    activity.gotoActivity(LoginActivity.class, null);
                })
                .setCancelable(false)
                .show();
    }

    @Override
    public void initTitle() {
        ivTitleBack.setVisibility(View.INVISIBLE);
        tvTitle.setText("我的资产");
    }

    @OnClick(R.id.iv_title_setting)
    public void setting(View view) {
        //启动用户信息界面的activity
        ((BaseActivity) this.getActivity()).gotoActivity(UserInfoActivity.class, null);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    public void onResume() {
        super.onResume();

        //读取本地保存的图片操作,该操作在设置头像后返回个人页面时执行
        readImage();
    }

    private boolean readImage() {

        File filesDir;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //判断sd卡是否挂载
            //路径1: storage/sdcard/Android/data/包名/files
            filesDir = this.getActivity().getExternalFilesDir("");
        } else {
            //手机内部存储
            //路径2: data/data/包名/files
            filesDir = this.getActivity().getFilesDir();
        }
        File file = new File(filesDir, "icon.png");

        if (file.exists()) {
            //存储  --> 内存
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            ivMeIcon.setImageBitmap(bitmap);
            return true;
        }
        return false;
    }

    //设置重置操作
    @OnClick(R.id.recharge)
    public void recharge(View view) {
        ((BaseActivity) this.getActivity()).gotoActivity(ChongZhiActivity.class, null);
    }

    //设置提现操作
    @OnClick(R.id.withdraw)
    public void withdraw(View view) {
        ((BaseActivity) this.getActivity()).gotoActivity(TiXianActivity.class, null);
    }

    @OnClick(R.id.ll_touzi)
    public void startLineChart(View view) {
        ((BaseActivity) this.getActivity()).gotoActivity(LineChartActivity.class, null);
    }

    @OnClick(R.id.ll_touzi_zhiguan)
    public void startBarChart(View view) {
        ((BaseActivity) this.getActivity()).gotoActivity(BarChartActivity.class, null);
    }

    @OnClick(R.id.ll_zichan)
    public void startPieChart(View view) {
        ((BaseActivity) this.getActivity()).gotoActivity(PieChartActivity.class, null);
    }
}
